/**
 * 
 */
package Domain;

import java.util.*;

/**
 * @author gth854p
 *
 */
public class RangeCommand implements RuleCommand {
	private Set<String> ruleSet;
	private String name;
	public RangeCommand()
	{
		char[] temp;
		setRuleSet(new HashSet<String>());
		
		this.name = "range";
		
	}
	public RangeCommand(String range)
	{
		int start,end;
		setRuleSet(new HashSet<String>());
		
		this.name = "range";
		
		char[] temp2;
		
		String[] temp = range.split("-");
		
		
		
		if(temp[0].startsWith("\\"))
			start = temp[0].charAt(1);
		else
			start = temp[0].charAt(0);
		if(temp[1].startsWith("\\"))
			end = temp[1].charAt(1);
		else
			end = temp[1].charAt(0);
		
		for(int i = start;i<end+1;i++)
		{
			
			if(i==92||i==94||i==91||i==93||i==45)
				{
					temp2 = new char[2];
					temp2[0] = '\\';
					temp2[1] = (char) i;
				}
			else
			{
				temp2  = new char[1];
				temp2[0] = (char) i;
			}
			
			getRuleSet().add(new String(temp2));
		}
	}
	
	
	/* (non-Javadoc)
	 * @see Domain.RuleCommand#checkSyntax(java.lang.String)
	 */
	@Override
	public boolean checkSyntax(String compare) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see Domain.RuleCommand#checkCorrectness(java.lang.String)
	 */
	@Override
	public boolean checkCorrectness(String compare) {
		int start,end;
		char[] temp2;
		if(!compare.contains("-"))
			return false;
		String[] temp = compare.split("-");
		CLS_CharCommand low = new CLS_CharCommand();
		if(!low.getRuleSet().contains(temp[0]))
			return false;
		if(!low.getRuleSet().contains(temp[1]))
			return false;
		
		if(temp[0].startsWith("\\"))
			start = temp[0].charAt(1);
		else
			start = temp[0].charAt(0);
		if(temp[1].startsWith("\\"))
			end = temp[1].charAt(1);
		else
			end = temp[1].charAt(0);
		if(end<start)
			return false;
		for(int i = start;i<end+1;i++)
		{
			
			if(i==92||i==94||i==91||i==93||i==45)
				{
					temp2 = new char[2];
					temp2[0] = '\\';
					temp2[1] = (char) i;
				}
			else
			{
				temp2  = new char[1];
				temp2[0] = (char) i;
			}
			
			getRuleSet().add(new String(temp2));
		}
		
		return true;
	}

	/* (non-Javadoc)
	 * @see Domain.RuleCommand#createNFA()
	 */
	@Override
	public NFADiagram createNFA() {
		// TODO Auto-generated method stub
		System.out.println("RangeCOMMAND");
		return new NFADiagram();
	}
	public void setRuleSet(Set<String> ruleSet) {
		this.ruleSet = ruleSet;
	}
	public Set<String> getRuleSet() {
		return ruleSet;
	}

}
